package com.cheyun.netsalev3.repository

import android.util.Log
import com.cheyun.netsalev3.bean.ClueDetails
import com.cheyun.netsalev3.bean.PrivacyPhone
import com.cheyun.netsalev3.bean.login.PhoneX
import com.cheyun.netsalev3.bean.records.Follow
import com.cheyun.netsalev3.bean.records.RecordsParam
import com.cheyun.netsalev3.utils.MySharedPreferences
import com.cheyun.netsalev3.utils.api.ApiCallback
import com.cheyun.netsalev3.utils.api.ApiClient
import com.cheyun.netsalev3.utils.api.ApiException
import com.cheyun.netsalev3.utils.api.BaseResp
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import org.json.JSONObject
import kotlin.reflect.KFunction1
import kotlin.reflect.KFunction2

class ClueDetailsRepository(var tid: Int, var nkey: String,var revkey:String) {
    /**
     * 获取详情
     */
    fun getData(ReturnData: KFunction2<@ParameterName(name = "item") ClueDetails, @ParameterName(name = "nkey") String, Unit>) {
        Log.e("getData", "getData.....")
        ApiClient.getInstance().getClueData(nkey, tid.toString(),revkey)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<ClueDetails>>() {
                    override fun onSuccess(t: BaseResp<ClueDetails>) {
                        t.data?.apply {
                            if(this.xphone.isNullOrEmpty()){
                                this.xphone = this.phone
                            }
                            ReturnData(this, nkey)
                        }

                        Log.e("onSuccess", "onSuccess.....")
                    }

                    override fun onError(ex: ApiException) {

                    }

                })
        Log.e("getInstance", "retun.....")
    }

    /**
     * 获取记录
     */
    fun getRecords(id:String, kFunction1: KFunction1<@ParameterName(name = "recordsParam") List<Follow>, Unit>) {
        ApiClient.getInstance().getRecords(id)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<List<Follow>>>() {
                    override fun onSuccess(t: BaseResp<List<Follow>>) {
                        t.data?.apply {
                            kFunction1(this)
                        }

                    }

                    override fun onError(ex: ApiException) {

                    }

                })
    }

    var pas = MySharedPreferences.getPassword()
    /**
     * 线索战败审核
     */
    fun getfailVerify(ids: String, confirmed: String, moth: (s:String,result:Int) -> Unit, owneruid: String = "", customerlevel: String = "") {
//     owneruid		线索接收者用户ID，confirmed为2时必须
//    customerlevel	客户等级ID，confirmed为2时必须
        var hashMap = HashMap<String, String>()
        hashMap["ids"] = ids
        hashMap["confirmed"] = confirmed
        if (!owneruid.isNullOrBlank()) {
            hashMap["owneruid"] = owneruid
        }
        if (!customerlevel.isNullOrBlank()) {
            hashMap["customerlevel"] = customerlevel

        }
        ApiClient.getInstance().getfailVerify(pas, hashMap)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<JSONObject>>() {
                    override fun onSuccess(t: BaseResp<JSONObject>) {
                        moth("确认成功",1)
                    }

                    override fun onError(ex: ApiException) {

                    }

                })
    }

    /**
     * 线索无效审核
     */
    fun getInvalidVerify(ids: String, confirmed: String, moth: (s:String,result:Int) -> Unit, owneruid: String = "", customerlevel: String = "") {
//     owneruid		线索接收者用户ID，confirmed为2时必须
//    customerlevel	客户等级ID，confirmed为2时必须
        var hashMap = HashMap<String, String>()
        hashMap["ids"] = ids
        hashMap["confirmed"] = confirmed
        if (!owneruid.isNullOrBlank()) {
            hashMap["owneruid"] = owneruid
        }
        if (!customerlevel.isNullOrBlank()) {
            hashMap["customerlevel"] = customerlevel

        }
        ApiClient.getInstance().getInvalidVerify(pas, hashMap)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<JSONObject>>() {
                    override fun onSuccess(t: BaseResp<JSONObject>) {
                        moth("确认成功",2)
                    }

                    override fun onError(ex: ApiException) {

                    }

                })
    }


    /**
     * 线索激活
     */
    fun getActivate(ids: String, moth: (s:String) -> Unit, owneruid: String, customerlevel: String) {
        var hashMap = HashMap<String, String>()
        hashMap["ids"] = ids
        hashMap["owneruid"] = owneruid
        hashMap["customerlevel"] = customerlevel
        ApiClient.getInstance().getActivate(pas, hashMap)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        object : ApiCallback<BaseResp<JSONObject>>() {
                            override fun onSuccess(t: BaseResp<JSONObject>) {
                                moth("激活成功")
                            }

                            override fun onError(ex: ApiException) {

                            }

                        })
    }

    fun getPrivacyPhone(phoneX: String,phoneB: String,tid: String, moth: (s:String) -> Unit) {
        ApiClient.getInstance().getPrivacyPhone(phoneX,phoneB,tid)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        object : ApiCallback<BaseResp<PrivacyPhone>>() {
                            override fun onSuccess(t: BaseResp<PrivacyPhone>) {
                                moth(t.data?.phonex!!)
                            }

                            override fun onError(ex: ApiException) {

                            }

                        })
    }
    fun setIsFocus(tid: String, isFocus: Int, moth: () -> Unit) {
        ApiClient.getInstance().isFocus(tid, if (isFocus == 1) "cancel" else "attention")
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<Any>>() {
                    override fun onSuccess(t: BaseResp<Any>) {
                        moth()
                    }

                    override fun onError(ex: ApiException) {

                    }


                })
    }

    fun getOwnerPhone(phoneB: String, moth: (List<PhoneX>) -> Unit) {
        ApiClient.getInstance().getPhoneList(phoneB)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        object : ApiCallback<BaseResp<List<PhoneX>>>() {
                            override fun onSuccess(t: BaseResp<List<PhoneX>>) {
                                moth(t.data!!)
                            }

                            override fun onError(ex: ApiException) {

                            }

                        })
    }

    /**
     * 线索移交
     */
    fun getHandover(key:String,tid:String, owneruid: String, customerlevel: String, remindetime: String, method: () -> Unit) {
        ApiClient.getInstance().getHandoverNew(key, tid, owneruid, customerlevel, remindetime)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<JSONObject>>() {
                    override fun onSuccess(t: BaseResp<JSONObject>) {
                        method()
                    }
                    override fun onError(ex: ApiException) {

                    }
                })
    }

}